package com.yuanyefu.jdbc;

import javax.swing.table.AbstractTableModel;
import java.util.List;

/**
 * Created by Administrator on 2017/9/18 0018.
 */
public class BookTableModel extends AbstractTableModel {
    private List<Book> bookList;
    private BookDao bookDao;

    public BookTableModel(BookDao bookDao) {
        this.bookDao = bookDao;
        this.bookList = bookDao.queryAll();
    }

    public void refresh(String author){
        if (author==null || author.trim().equals(""))
            bookList=bookDao.queryAll();
        else
            bookList=bookDao.queryByAuthor(author.trim());
        fireTableDataChanged();
    }
    public void removeBook(Book book){
        bookDao.delete(book.getId());
        bookList.remove(book);
        fireTableDataChanged();
    }
    public void updateBook(Book book){
        bookDao.update(book);
        fireTableDataChanged();
    }
    public void createBook(Book book){
        bookDao.create(book);
        refresh(null);
    }
    @Override
    public int getRowCount() {
        return 0;
    }

    @Override
    public int getColumnCount() {
        return 0;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return null;
    }
}
